# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def rob(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        return max(self.help(root))

    def help(self, node):
        if not node:
            return 0, 0
        leftv, noleftv = self.help(node.left)
        rightv, norightv = self.help(node.right)
        #         偷当前的
        robcur = node.val + noleftv + norightv
        #         不偷
        norobcur = max(leftv, noleftv) + max(rightv, norightv)
        return robcur, norobcur